package game.players.goalkeeper.opp;

import java.awt.Point;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Animation;
import com.uqbar.vainilla.appearances.Appearance;
import com.uqbar.vainilla.appearances.Sprite;

import game.players.SoccerPlayer;
import game.players.goalkeeper.GoalkeeperState;

public class WalkingToTheRightOpp extends GoalkeeperState {
	
//	private double i = 0;
//	private double j = 0;
	private Point destiny = null;
	private Appearance appearance = new Animation(0.3,
			Sprite.fromImage("/images/arquero_arco.png").crop(21,11,84,77).flipHorizontally(),
			Sprite.fromImage("/images/arquero_arco.png").crop(112,13,84,77).flipHorizontally(),
			Sprite.fromImage("/images/arquero_arco.png").crop(200,13,84,79).flipHorizontally());

	public WalkingToTheRightOpp(SoccerPlayer player) {
		super(player);
		player.setAppearance(this.appearance);
	}

	public WalkingToTheRightOpp(SoccerPlayer player, Point p) {
		super(player);
		player.setAppearance(this.appearance);
		this.destiny = p;
	}

	@Override
	public void update(DeltaState deltaState) {
		if(this.destiny==null){//camina hacia la derecha hasta pasar la linea de la pelota
			this.getPlayer().move(10,0);
			if(this.getPlayer().getScene().getSoccerBall().getX() < this.getPlayer().getX())
				this.getPlayer().setState(new GoalkeeperWithoutBallOpp((GoalkeeperCPUOpp)this.getPlayer()));
		}else{//camina hacia la derecha hacia un punto de destino
			this.advanceToDestiny();
			if(Point.distance(this.getPlayer().getX(),this.getPlayer().getY(),
					this.destiny.getX(),this.destiny.getY()) < 200)
				this.getPlayer().setState(new GoalkeeperWithoutBallOpp(this.getGoalkeeper()));
		}
	}
	
	public void advanceToDestiny() {
		double vectorI = this.destiny.getX() - this.getPlayer().getX() + this.getPlayer().getFeetWidth();
		double vectorJ = this.destiny.getY() - this.getPlayer().getY() - this.getPlayer().getAppearance().getHeight() + this.getPlayer().getFeetHeight();		
		double module = Math.sqrt(Math.pow(vectorI, 2) + Math.pow(vectorJ, 2));		
		double versorI = vectorI / module;
		double versorJ = vectorJ / module;		
		this.getPlayer().move(versorI * this.getPlayer().getMovementSpeed() / 2, versorJ * this.getPlayer().getMovementSpeed() / 2);
	}

	@Override
	public boolean isWithBall() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isRight() {
		// TODO Auto-generated method stub
		return true;
	}


}
